#include "StdAfx.h"
#include "RuleMgr.h"
#include "dao/ruledao.h"
#include "dao/DAOFactory.h"

CRuleMgr::CRuleMgr(void):m_bInit(false)
{
	m_pRuleDao = CDAOFactory::GetRuleDao();
}

CRuleMgr::~CRuleMgr(void)
{
	if(m_pRuleDao)
		delete m_pRuleDao;
}



BOOL  CRuleMgr::AddARule(CRule*pRule)
{
	bool bOk;
	if(pRule == NULL)
		return false;
	do 
	{
		if(!m_pRuleDao->AddARule(*pRule))
		{	
			bOk= false;
			delete pRule;
			break;
		}
		m_ruleInfoVector.push_back(pRule);
		bOk = true;
	} while (false);
	return bOk;
}


BOOL CRuleMgr::DelARule(CRule*pRule)
{

	if(pRule == NULL)
		return false;

	bool bOk= false;
	vector<CRule*>::iterator  it;
	do 
	{

		if(!m_pRuleDao->DelARule(pRule->m_ruleId))
		{
			break;
		}

		for(it = m_ruleInfoVector.begin();it != m_ruleInfoVector.end();it++)
		{
			if((*it )== pRule )
			{
				m_ruleInfoVector.erase(it);
				break;
			}
		}

		delete pRule;
		bOk = true;

	} while (FALSE);

	return bOk;

}



void CRuleMgr::LoadRuleInfo()
{
	int i = 0;
	CRule      *rule;
	
	if(m_bInit)
		return;
	do 
	{
	
		if(!m_pRuleDao->GetAllRule())
		{
			break;
		}
		rule = new CRule;
		while(m_pRuleDao->EnumRule(*rule))
		{
			m_ruleInfoVector.push_back(rule);
			rule = new CRule;
		}
		if(rule)
			delete rule;
	} while (FALSE);

	m_bInit = true;

}



void CRuleMgr::Clear()
{
	CRule*  pRule = NULL;
	while(m_ruleInfoVector.size())
	{
		pRule = m_ruleInfoVector.back();
		m_ruleInfoVector.pop_back();
		if(pRule)
			delete  pRule;
	}
	m_bInit = false;
}